#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 18 juin 2011

@author: nathanael
'''


class Lingual(object):
    """Sort language support.
    """
    __slots__ = ('names', '_case', '_emphasis', '_books_name', '_books_abv')
    def __init__(self, names, case_sensitive, emphasis_characters,
                 books_name=None, books_abv=None):
        
        self.names = names
        self._case = case_sensitive
        self._emphasis = emphasis_characters
        self._books_name = books_name
        self._books_abv = books_abv
        
    def get_books_name(self, short=False):
        if short:
            return self._books_abv
        else:
            return self._books_name
        
    def has_case(self):
        "Retrieve is the language is case sensitive."
        
        return self._has_case
    
    def has_emphasis(self):
        "Retrieve if the language has emphasis characters."
        
        return len(self._emphasis) > 0
     
    def get_emphasis(self):
        """Return the list of equivalent emphasis characters in 'tuple'. The 
        first  char has any emphasis ('eéèê', 'aà', ...)"""
    
        return self._emphasis
    

class Linguals(object):
    """Sort languages support.
    """
    
    def __init__(self, *languages):
        
        self._list_languages = languages
        self.build_htable()
        
    def build_htable(self):
        """Build htable to quick find language.
        """
        
        htable = {}
        for l in self._list_languages:
            for n in l.names:
                if htable.has_key(n):
                    raise ValueError("Multi record for language name %s" % n)
                htable[n] = l
        self._htable = htable
                
    def __getitem__(self, language):
        "Return language support."
        
        return self._htable[language]
    
    def has_support_for(self, language):
        ""
        
        return self._htable.has_key(language)



LINGUALS = Linguals(Lingual(('french', 'français', 'fr'),
                            case_sensitive=True,
                            emphasis_characters=('eéèêë', 'aà', 'uù')),
                    )

def has_langual(language):
    return LINGUALS.has_support_for(language)

def get_langual(language):
    return LINGUALS[language]

